package net.tycmc.bulb.note.core.Util;

import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.util.MapGetter;

import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushUnicastMessageRequest;
import com.baidu.yun.channel.model.PushUnicastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;

public class OldMsgTobaidyun {

	public void sendMsg(List<Map<String,Object>> list,Map<String,Object> configMap, String apiKey, String secretKey){
        /*
         * @brief 推送单播通知(Android Push SDK拦截并解析) message_type = 1 (默认为0)
         */
        // 1. 设置developer平台的ApiKey/SecretKey
        //String apiKey = MapGetter.getString(configMap,"apiKey");
        //String secretKey = MapGetter.getString(configMap,"secretKey");
      //获取频道id
		Long channelId ;
		//获取手机端userid
		String userId = "";
		//获取产生故障的设备号
		String vehicleNumber = "";
		//获取故障码
		String fltCode = "";
		//获取故障详情
		String fltDetial = "";
		//获取风险信息
		String riskSuggest = "";
		//拼接标题
		String title = "";
		//故障发生时间
		String msgtime="";
		//推送类型
		String type = "";
		//故障等级1、2、3、4
		String level="";
		//拼接内容
		String description = fltDetial+";"+riskSuggest;
		//要发送的内容
		String sendMsg="";
		//发送语言环境1：英文，0：中午
		String lan = "";
		
        ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);

        // 3. 若要了解交互细节，请注册YunLogHandler类
        channelClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {

        	//遍历list集合
        	for(int i = 0; i < list.size(); i++){
        		Map<String, Object> msgData = list.get(i);
        		lan=msgData.get("lan").toString();
        		//获取频道id
        		channelId = Long.parseLong(msgData.get("chanlid").toString());
        		//获取手机端userid
        		userId = (String) msgData.get("userid");
        		//获取产生故障的设备号
        		vehicleNumber = (String) msgData.get("vehiclenumber");
        		//获取故障码
        		fltCode = (String) msgData.get("fltcode");
        		//获取故障详情
        		if(lan.equals("1")){//英文
        			fltDetial = (String) msgData.get("fltdetialmsg_EN");
        			//拼接标题
            		title = vehicleNumber+"Fault"+fltCode;
            		riskSuggest = (String) msgData.get("risk_EN");
        		}else{
        			fltDetial = (String) msgData.get("fltdetialmsg");
        			//拼接标题
            		title = vehicleNumber+"故障"+fltCode;
            		//获取风险信息
            		riskSuggest = (String) msgData.get("risk");
        		}
        		//故障时间
        		msgtime=(String) msgData.get("msgtime");
        		//拼接内容
        	    description = fltDetial+";"+riskSuggest+";"+msgtime;
        		//推送类型
        		type = "1";
        		//故障等级
        		level=(String) msgData.get("level");
        		//发送内容
        		//sendMsg="{'title':'"+title+"','description':'"+description+"','type':'"+type+"','level':'"+level+"','msgtime':'"+msgtime+"'}";
        		sendMsg="{'title':'"+title+"','description':'"+description+"'}";
        	    //如果channelId和userId有一个为空则进行下一次循环
        		if(channelId.equals("") || channelId==null || userId.equals("") || userId==null){
        			continue;
        		}
	            // 4. 创建请求类对象
	            // 手机端的ChannelId， 手机端的UserId， 先用1111111111111代替，用户需替换为自己的
	            PushUnicastMessageRequest request = new PushUnicastMessageRequest();
	            request.setDeviceType(3); // device_type => 1: web 2: pc 3:android
	                                  // 4:ios 5:wp
	            request.setMessageType(1);
	            request.setChannelId(channelId);
	            request.setUserId(userId);
	
	           // request.setMessageType(1);
	            //request.setMessage("{\"title\":"+title+",\"description\":"+description+",\"type\":"+type+"}");
	            request.setMessage(sendMsg);
	
	            // 5. 调用pushMessage接口
	            PushUnicastMessageResponse response = channelClient
	                    .pushUnicastMessage(request);
	
	            // 6. 认证推送成功
	            System.out.println("push amount : " + response.getSuccessAmount());
        	}
        } catch (ChannelClientException e) {
            // 处理客户端错误异常
            e.printStackTrace();
        } catch (ChannelServerException e) {
            // 处理服务端错误异常
            System.out.println(String.format(
                    "request_id: %d, error_code: %d, error_message: %s",
                    e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
        }
	}
}
